/// describe
///
/// created by hujintao
/// created at 2021/5/18
//
import 'dart:convert';
import 'dart:ui';
import 'package:connectivity/connectivity.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutterappdriver/pages/home/home_page.dart';
import 'package:flutterappdriver/routes/Routes.dart';
import 'package:flutterappdriver/utils/adaption_util.dart';
import 'package:flutterappdriver/utils/navigator_util.dart';

/// app 启动页
///
/// 启动流程看: https://www.yuque.com/wuxiekeji/dqd/ae8tto
class WelcomePage extends StatefulWidget {
  @override
  _WelcomePageState createState() => _WelcomePageState();
}

class _WelcomePageState extends State<WelcomePage>
    with TickerProviderStateMixin {

  String version = '';

  /* 动画相关 */
  late AnimationController animation1Controller;
  late CurvedAnimation curvedAnimation1;
  Duration animation1Duration = const Duration(milliseconds: 300);

  late AnimationController animation2Controller;
  late CurvedAnimation curvedAnimation2;

  late AnimationController animation3Controller;
  late CurvedAnimation curvedAnimation3;

  bool animation2 = false;

  AnimationController get animationController {
    return this.animation2
        ? this.animation2Controller
        : this.animation3Controller;
  }

  /* 动画相关 */
  late Function enterMethod;

  int reStartCnt = 0;

  String token = "";


  @override
  void initState() {
    super.initState();

    this.animation1Controller =
        AnimationController(vsync: this, duration: this.animation1Duration);
    this.curvedAnimation1 = new CurvedAnimation(
        parent: this.animation1Controller, curve: Curves.ease);
    this.animation1Controller.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        Future.delayed(Duration(milliseconds: 100), () {
          this.animation2Controller.forward();
        });
      }
    });

    this.animation2Controller = AnimationController(
        vsync: this, duration: const Duration(milliseconds: 570));
    this.curvedAnimation2 = new CurvedAnimation(
        parent: this.animation2Controller, curve: Curves.ease);
    this.animation2Controller.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        Future.delayed(Duration(milliseconds: 280), () {
          this.animation2 = true;
          this.animation3Controller.forward();
          setState(() {});
        });
      }
    });

    this.animation3Controller = AnimationController(
        vsync: this, duration: const Duration(milliseconds: 150));
    this.curvedAnimation3 = new CurvedAnimation(
        parent: this.animation3Controller, curve: Curves.ease);
    this.animation3Controller.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        this.enterMethod();
      }
    });
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    this.init();
  }

  @override
  void dispose() {
    this.animation1Controller?.dispose();
    this.animation2Controller?.dispose();
    this.animation3Controller?.dispose();

    super.dispose();
  }



  void init ({bool isReStart = false}) async {

    bool continueNext = true;


    this.enterFirstPage();
  }



  /// 进入第一个页面
  void enterFirstPage() async {
    try {
      this.animation1Controller.forward();
    } catch (e) {}
    this.enterMethod = () {
      Navigator.pushReplacementNamed(context, Routes.home_page);
    };
    // Navigator.of(context).pushNamed(Routes.home_page);
    //  NavigatorUtil.push(context, HomePage());

  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: Stack(
        children: <Widget>[
          Positioned(
            top: MediaQuery.of(context).size.height * .4,
            left: 0,
            right: 0,
            child: AnimatedBuilder(
              animation: this.curvedAnimation1,
              builder: (_, child) {
                return Opacity(
                  opacity: 1 - this.curvedAnimation1.value,
                  child: child,
                );
              },
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  // gif
                  Image.asset(
                    'assets/images/logo_loading.gif',
                    width: AdaptationUtils.setWidth(303),
                  ),

                  Container(
                    margin: EdgeInsets.only(top: AdaptationUtils.setWidth(20)),
                    child: Image.asset(
                      'assets/images/slogan_grey.png',
                      width: AdaptationUtils.setWidth(282),
                    ),
                  )
                ],
              ),
            ),
          ),
          Center(
              child: AnimatedBuilder(
                animation:
                this.animation2 ? this.curvedAnimation3 : this.curvedAnimation2,
                builder: (_, child) => Container(
                    transform: Matrix4.translationValues(
                        0,
                        AdaptationUtils.setWidth(this.animation2
                            ? this.curvedAnimation3.value * -95
                            : (1 - this.curvedAnimation2.value) * 50),
                        0.0),
                    child: Opacity(
                      opacity: this.animation2
                          ? (1 - this.curvedAnimation3.value)
                          : this.curvedAnimation2.value,
                      child: Image.asset(
                        'assets/images/welcome_content.png',
                        width: AdaptationUtils.setWidth(536),
                      ),
                    )),
              )),
          Positioned(
            width: MediaQuery.of(context).size.width,
            bottom: MediaQuery.of(context).size.height * 0.05,
            child: Text(
              'v ${this.version}',
              style: TextStyle(fontSize: 12, color: Color(0xFFdddddd)),
              textAlign: TextAlign.center,
            ),
          ),
        ],
      ),
    );
  }
}